/**
 * @description: To calculate the differences of the max elememnt of an array and the min of the
 * elements before max
 * @author: timlentse(tinglenxan@gmail.com)
 * @Date:2014-11-13
 */
#include <iostream>
int solution( int* const &p, int length )
{
	if ( length == 0 || length == 1 )
	{
		return(-1);
	}
	int indexOfMin = 0, indexOfMax = 0, indexOfTmp = 0;
	for ( int i = 1; i < length; ++i )
	{
		if ( p[indexOfMax] < p[i] )
		{
			indexOfMax	= i;
			indexOfMin	= indexOfTmp;
		}else  {
			indexOfTmp = p[indexOfTmp] > p[i] ? i : indexOfTmp;
		}
	}
	return(indexOfMax > indexOfMin ? p[indexOfMax] - p[indexOfMin] : -1);
}


int main( int argc, char* argv[] )
{
	int	testArray[]	= { 9, 76, 78, 76, -1, 3, 2, 1, 76, 4, 5, 6 };
	int	* pointer	= testArray;
	std::cout << solution( pointer, sizeof(testArray) / sizeof(int) );
	return(0);
}


